package com.example.yangxu0703.thundermapclient.bluetooth;

import android.bluetooth.BluetoothSocket;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

public class ReciveMsg extends Thread {

    private BluetoothSocket socket;
    private InputStream in;
    private AbsBluetooth.Listerner listerner;

    public ReciveMsg(BluetoothSocket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        /*if (!socket.isConnected()) {
            try {
                socket.connect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        if (socket.isConnected()) {
            try {
                in = socket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean isreading = true;
        byte[] lengthBuffer = new byte[1024 * 1024];
        try {
            in.read(lengthBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = new String(lengthBuffer);
        Log.i("CON_STATE", result + "recive MSG");
        if (listerner != null) {
            listerner.backMessage(result);
        }
    }

    public void setListerner(AbsBluetooth.Listerner listerner) {
        this.listerner = listerner;
    }

}